package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.GuideTable;
import com.qf.service.GuideTableService;
import com.qf.util.R;
import org.apache.catalina.LifecycleState;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * (GuideTable)表控制层
 *
 * @author ZhiHuiJun
 * @since 2024-05-04 01:43:24
 */
@RestController
@RequestMapping("/api/guideTable")
public class GuideTableController {
    /**
     * 服务对象
     */
    @Autowired
    private GuideTableService guideTableService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<GuideTable> list = guideTableService.selectGuideTableByCondition(map);
        if (list != null && list.size() > 0){
            PageInfo<GuideTable> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }
        return R.fail();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.guideTableService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param guideTable 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public R add( GuideTable guideTable) {
       int i = guideTableService.insert(guideTable);
       if (i>0){
           return R.ok();
       }
        return R.fail();
    }

    /**
     * 编辑数据
     *
     * @param guideTable 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public R edit( GuideTable guideTable) {
       boolean flag = guideTableService.update(guideTable);
        if (flag){
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
      boolean flag = guideTableService.deleteById(id);
        if (flag) {
            return R.ok();
        }
        return R.fail();
    }

    @RequestMapping("/del/batch")
    public R delBatch(@RequestParam List<Integer> list){
       int i = guideTableService.delBatch(list);
       if (i>0){
           return R.ok();
       }
       return R.fail();
    }

}

